﻿

function searchItem() {



    var jData = {};

    jData.Brand = $("#searchbar").val();

    $.ajax({
        type: "POST",
        url: "ProductService.svc/searchProducts",
        data: JSON.stringify(jData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (json) {
            var data = JSON.parse(json.d);
            $("#Div1").html("");
            for (start = 0; start < data.binding.length; start++) {
                var jsonProduct = "{Product:'" + data.binding[start].ProductID + "',Brand:'" + data.binding[start].Brand + "',Model:'" + data.binding[start].Model + "',Price:'" + data.binding[start].Price + "',File:'" + data.binding[start].Filepath + "'}"
                var imgPath = '<img width="100%" height="100px" src="' + data.binding[start].Filepath + '"/>'
                var link = '<a href="#" onclick=window.open("moreDetails.aspx?proid=' + data.binding[start].ProductID + '",titlebar=0,toolbar=0,status=0,menubar=0)>More Details</a>';
                var addToCart = '<input class="btn btn-primary" type="submit" value="Add To Cart" onclick="return addOrderItem(' + jsonProduct + ')" />';

                var brk = '<br>';
                //return "<div> " + imgPath + " Brand Name : " + Details[0] + "  Model : " + Details[1] + " Price : " + Details[2] + "" + link + " </div>";

                var newitem = '<div class="gridWrap"> <div class="gridImage" > ' + imgPath + ' </div> <div class="gridProductInformation" > Product ID : '
                             + data.binding[start].ProductID + brk + ' Brand Name : '
                             + data.binding[start].Brand + brk + ' Model : '
                             + data.binding[start].Model + brk + ' Price :<span class="price"> '
                             + data.binding[start].Price + brk + '</span> </div>  <div class="gridLink">' + addToCart + ' <div class="moreDetails">' + link + '<div> <div>   </div>';

                $("#Div1").append(newitem);



            }
            //$("#Div1").hide().html(newitem).fadeIn('fast');
        },
        failure: function (msg) {
            alert(msg);
        },
        error: function (jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
    return false;


}

    
